
<html>
	<head>
		<script type="text/javascript" language="javascript" src="<?=base_url()?>/media/js/jquery.js"></script>

		<script type="text/javascript">

			$(document).ready(function(){
				$('.del').live('click', function(event){
				   $(this).parent().remove();
				});
				
				//Select
				var select_count =1;
				
				$('.select #op').hide(); //TP
				$('.select .add').click(function(){
					var newNum = select_count++;
					
					$clones = $('.select #op').clone().attr('id', 'op'+newNum).show();
					$('.select .add').before($clones);
					
				});
				
				//where
				var where_count =1;
				
				$('.where #op').hide(); //TP
				$('.where .add').click(function(){
					var newNum = where_count++;
					
					$clones = $('.where #op').clone().attr('id', 'op'+newNum).show();
					$('.where .add').before($clones);
					
				});
				
				
				//group by
				var group_count =1;
				
				$('.group #op').hide(); //TP
				$('.group .add').click(function(){
					var newNum = group_count++;
					
					$clones = $('.group #op').clone().attr('id', 'op'+newNum).show();
					$('.group .add').before($clones);
					
				});
				
				
				//having
				var having_count =1;
				
				$('.having #op').hide(); //TP
				$('.having .add').click(function(){
					var newNum = having_count++;
					
					$clones = $('.having #op').clone().attr('id', 'op'+newNum).show();
					$('.having .add').before($clones);
					
				});
				
				
				//Order By
				var order_count =1;
				
				$('.order #op').hide(); //TP
				$('.order .add').click(function(){
					var newNum = order_count++;
					
					$clones = $('.order #op').clone().attr('id', 'op'+newNum).show();
					$('.order .add').before($clones);
					
				});

				//Create Field Name ListBox Item
				var ListBoxItem = $('<?php echo $rawdata;?>');
				$(ListBoxItem).appendTo('.listField');
				
				//Create RawData Choice for Select
				
				
				//Create KPI Choice for Select
				
				
				//Create Function for SELECT Only
				var SelectFuncItem = $('<option value="">none</option><option value="SUM(">SUM</option><option value="AVG(">AVG</option><option value="MIN(">MIN</option><option value="MAX(">MAX</option><option value="COUNT(DISTINCT ">COUNT[DISTINCT]</option><option value="COUNT(">COUNT</option>');
				$(SelectFuncItem).appendTo('.FuncSelect');
				
				//Create Conditon for WHERE HAVING
				var CondItem = $('<option value="">-Condition-</option><option value="=">=</option><option value=">">\></option><option value=">=">\>=</option><option value="<">\<</option><option value="<=">\<=</option><option value="!=">!=</option><option value="LIKE">LIKE</option><option value="NOT LIKE">NOT LIKE</option><option value="IS NULL">IS NULL</option><option value="IS NOT NULL">IS NOT NULL</option><option value="IN">IN</option><option value="NOT IN">NOT IN</option>');
				$(CondItem).appendTo('.FuncCondition');	
				
				//Chain Change Select
				$('#list_select').live('change', function(event){
				   
					if($(this).find(':selected').hasClass('kpi')){
						$(this).next().val($(this).val());
						$(this).next().next().next().val($(this).find(':selected').text());
					}
					else if($(this).find(':selected').hasClass('rawdata')){
						$(this).next().val($(this).find(':selected').text());
						$(this).next().next().next().val('');
					
					}
					else{
						$(this).next().val('').show();
						$(this).next().next().next().val('');


					}
				});
				
				//Chain Change Where
				$('#list_where').live('change', function(event){
				   
					if($(this).find(':selected').hasClass('kpi')){
						$(this).next().val($(this).val());
						$(this).next().next().next().val($(this).find(':selected').text());
					}
					else if($(this).find(':selected').hasClass('rawdata')){
						$(this).next().val($(this).find(':selected').text());
						$(this).next().next().next().val('');
					
					}
					else{
						$(this).next().val('').show();
						$(this).next().next().next().val('');


					}
				});
				
				//Chain Change group
				$('#list_group').live('change', function(event){
				   
					if($(this).find(':selected').hasClass('kpi')){
						$(this).next().val($(this).find(':selected').text());
					}
					else if($(this).find(':selected').hasClass('rawdata')){
						$(this).next().val($(this).find(':selected').text());					
					}
					else{
						$(this).next().val('').show();


					}
				});
				
				//Chain Change Having
				$('#list_having').live('change', function(event){
				   
					if($(this).find(':selected').hasClass('kpi')){
						$(this).next().val($(this).val());
						$(this).next().next().next().val($(this).find(':selected').text());
					}
					else if($(this).find(':selected').hasClass('rawdata')){
						$(this).next().val($(this).find(':selected').text());
						$(this).next().next().next().val('');
					
					}
					else{
						$(this).next().val('').show();
						$(this).next().next().next().val('');


					}
				});

				//Chain Change Order
				$('#list_order').live('change', function(event){
				   
					if($(this).find(':selected').hasClass('kpi')){
						$(this).next().val($(this).find(':selected').text());
					}
					else if($(this).find(':selected').hasClass('rawdata')){
						$(this).next().val($(this).find(':selected').text());					
					}
					else{
						$(this).next().val('').show();
					}
				});

			
			});
		</script>
	
	</head>
	
	<body>
	
		<form name="filter" method="post" action="">
		
			<fieldset class="select">
				<legend>Select</legend>
		
				<input class="ChoiceRawData" type="checkbox" name="kpichoice" value="checkselect"><br>
				<input class="ChoiceKPI" type="checkbox" name="kpichoice" value="checkselect"><br>
	
				<div class="option_select" id='op'>
					<select name="listField" class="listField" id="list_select">
					
			    	</select>

					<input type="text" name="formula" id="formula">
			    
					<select name="Func" class="FuncSelect">
					</select>
					
					<input type="text" name="newname" id="newname">
					<label class="del">Remove</label>
				</div>
				<label class="add">Add</label>
			</fieldset>	

		    <fieldset class="where">
				<legend clas='where'><input type="checkbox" name="checkwhere" class="where">Where</legend>
			
				<div class="where_condition" id='op'>
					<select name="condition" id="conditon">	
						<option value=""></option>
						<option value="AND">AND</option>
						<option value="OR">OR</option>
						<option value="NOT">NOT</option>
					</select>
					
					<select name="listField" class="listField" id="list_where">
			    	</select>
			    	
					
					<input type="text" name="formula" id="formula">

			    
					<select name="Func" class="FuncWhere">	
					</select>
				
					<input type="text" name="values" id="values">
					<label class="del">Remove</label>
				
				</div>
				<label class="add">Add</label>
			</fieldset>
			
			<fieldset class="group">
				<legend><input type="checkbox" name="checkgroup" class="group">Group By</legend>
			
				<div class="gorup_condition" id='op'>
					<select name="listField" class="listField" id="list_group">
			    	</select>
			    	
			    	<input type="text" name="formula" id="formula">

			    	
					<label class="del">Remove</label>
				</div>
				<label class="add">Add</label>
			</fieldset>

			<fieldset class="having">
				<legend><input type="checkbox" name="checkhaving" class="having">Having</legend>
			
				<div class="having_condition" id='op'>
					<select name="condition" id="conditon">	
						<option value=""></option>
						<option value="AND">AND</option>
						<option value="OR">OR</option>
						<option value="NOT">NOT</option>
					</select>
					
					<select name="listField" class="listField" id="list_having">
			    	</select>
			    	
					
					<input type="text" name="formula" id="formula">

			    
					<select name="Func" class="FuncWhere">	
					</select>
				
					<input type="text" name="values" id="values">
					<label class="del">Remove</label>
				
				</div>
				<label class="add">Add</label>
			</fieldset>
		   

			<fieldset class="order">
				<legend><input type="checkbox" name="checkorder" class="having">Order By</legend>
			
				<div class="field_select" id='op'>
					<select name="listField" class="listField" id="list_order">
			    	</select>
			    
					<input type="text" name="formula" id="formula">

					<select name="functionorder" id="FunOrder">
						<option value="ASC">ASC</option>
  						<option value="DSC">DSC</option>	
					</select>
					<label class="del">Remove</label>
				</div>
				<label class="add">Add</label>
			</fieldset>
		  
	
		 
		
		    <input type="submit" name="Submit" value="Submit">
		
		</form>
	</body>
</html>
